package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.po.Nav;
import com.example.demo.service.NavService;



@Controller
@RequestMapping("/admin")
public class AdminNavController {
	
	
	@Autowired
	private NavService navService;
	
	@RequestMapping(value="/adminNav.action",method=RequestMethod.GET)
	public String index(Model model){
		
		List<Nav> navList = navService.selectAll();
		
		model.addAttribute("navList", navList);
		
		return "admin/nav/navList";
		
	}
	
	@RequestMapping(value="/toEditNav.action",method=RequestMethod.GET)
	public String toEdit(Model model,Integer id){
		
		if(id!=null){
			
			Nav nav=navService.selectById(id);
			
			model.addAttribute("nav", nav);
		}
				
		return "admin/nav/edit";
		
	}
	
	@RequestMapping(value="/editNav.action",method=RequestMethod.POST)
	public String edit(Nav nav){
		
		if(nav.getId()!=null){
			navService.update(nav);
		}else{
			navService.insert(nav);
		}
		
		nav.setStatus(1);
		
		return "redirect:adminNav.action";
	}
	
	@RequestMapping(value="/deleteNav.action",method=RequestMethod.GET)
	public String delete(Integer id){
		
		navService.delete(id);;
		return "redirect:adminNav.action";
	}
}
